package org.osll.tictactoe.transport.corba.server;

import org.osll.tictactoe.engine.SizeImpl;
import org.osll.tictactoe.game.Game;
import org.osll.tictactoe.transport.corba.Adapter;
import org.osll.tictactoe.transport.corba.service.ControlPOA;
import org.osll.tictactoe.transport.corba.service.CurrentMove;
import org.osll.tictactoe.transport.corba.service.ErrorException;
import org.osll.tictactoe.transport.corba.service.Move;
import org.osll.tictactoe.transport.corba.service.Size;
import org.osll.tictactoe.transport.corba.service.Team;

public class ControlServant extends ControlPOA {

	private Game controller = null;
	
	public ControlServant(Game controller) {
		this.controller = controller;
	}

	@Override
	synchronized public void disconnect() throws ErrorException {
		// ничего		
	}

	@Override
	synchronized public String getApponenName(Team team) throws ErrorException {
		try {
			return controller.getApponentName(Adapter.convertTeam(team));
		} catch (IllegalStateException e) {
			ErrorException ex = new ErrorException();
			ex.description = Adapter.makeString(e);
			throw ex;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	@Override
	synchronized public Move getApponentMove(Team team) throws ErrorException {
		try {
			return Adapter.convertMove(controller.getLastMove(Adapter.convertTeam(team)));
		} catch (IllegalStateException e) {
			ErrorException ex = new ErrorException();
			ex.description = Adapter.makeString(e);
			throw ex;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	synchronized public CurrentMove getMoveEndTime() throws ErrorException {
		try {	
			return Adapter.convertCurrentMove(controller.getCurrentMove());
		} catch (IllegalStateException e) {
			ErrorException ex = new ErrorException();
			ex.description = Adapter.makeString(e);
			throw ex;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	synchronized public Size getSize() throws ErrorException {
		try {
			SizeImpl s = new SizeImpl();
			s.setDimX(controller.getField().getSize().getWidth());
			s.setDimY(controller.getField().getSize().getHeight());
			return Adapter.convertSize(s);
		} catch (IllegalStateException e) {
			ErrorException ex = new ErrorException();
			ex.description = Adapter.makeString(e);
			throw ex;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	synchronized public boolean isOver() throws ErrorException {
		try {
			return controller.isOver();
		} catch (IllegalStateException e) {
			ErrorException ex = new ErrorException();
			ex.description = Adapter.makeString(e);
			throw ex;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	synchronized public boolean isStarted() throws ErrorException {
		try {
			return controller.isStarted();
		} catch (IllegalStateException e) {
			ErrorException ex = new ErrorException();
			ex.description = Adapter.makeString(e);
			throw ex;
		}catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	synchronized public void makeMove(Move move) throws ErrorException {
		try {
			controller.makeMove(Adapter.convertMove(move));
		} catch (IllegalStateException e) {
			ErrorException ex = new ErrorException();
			ex.description = Adapter.makeString(e);
			throw ex;
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	synchronized public void connect(String userName, Team team) throws ErrorException {
		try {
			controller.loginPlayer(userName, Adapter.convertTeam(team));
		} catch (IllegalStateException e) {
			ErrorException ex = new ErrorException();
			ex.description = Adapter.makeString(e);
			throw ex;
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
}
